import pygame
import math
import random

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("篮球投篮游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)

# 篮球属性
ball_radius = 15
ball_color = ORANGE
ball_x = WIDTH // 4
ball_y = HEIGHT - 200
ball_speed_x = 0
ball_speed_y = 0
gravity = 0.5

# 篮筐属性
hoop_width = 80
hoop_height = 5
hoop_x = WIDTH - 200
hoop_y = 200
score = 0

# 拖拽相关变量
dragging = False
drag_start_pos = (0, 0)

# 字体设置
font = pygame.font.Font(None, 36)

def draw_ball():
    pygame.draw.circle(win, ball_color, (int(ball_x), int(ball_y)), ball_radius)

def draw_hoop():
    # 绘制篮筐
    pygame.draw.rect(win, BLACK, (hoop_x, hoop_y, hoop_width, hoop_height))
    pygame.draw.rect(win, BLACK, (hoop_x, hoop_y - 20, 10, 20))
    pygame.draw.rect(win, BLACK, (hoop_x + hoop_width - 10, hoop_y - 20, 10, 20))

def reset_ball():
    global ball_x, ball_y, ball_speed_x, ball_speed_y
    ball_x = WIDTH // 4
    ball_y = HEIGHT - 200
    ball_speed_x = 0
    ball_speed_y = 0

def check_score():
    global score
    # 检测篮球是否通过篮筐
    if (hoop_x < ball_x < hoop_x + hoop_width) and \
       (hoop_y - ball_radius < ball_y < hoop_y + hoop_height + ball_radius):
        score += 1
        reset_ball()

# 游戏主循环
running = True
while running:
    win.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 检测鼠标是否点击在篮球上
            mouse_x, mouse_y = pygame.mouse.get_pos()
            distance = math.hypot(ball_x - mouse_x, ball_y - mouse_y)
            if distance <= ball_radius:
                dragging = True
                drag_start_pos = pygame.mouse.get_pos()

        elif event.type == pygame.MOUSEBUTTONUP and dragging:
            dragging = False
            # 计算投掷力度和方向
            end_pos = pygame.mouse.get_pos()
            ball_speed_x = (drag_start_pos[0] - end_pos[0]) / 10
            ball_speed_y = (drag_start_pos[1] - end_pos[1]) / 10

        elif event.type == pygame.MOUSEMOTION and dragging:
            # 拖拽时显示力度线
            pass

    # 更新篮球位置
    if not dragging:
        ball_speed_y += gravity
        ball_x += ball_speed_x
        ball_y += ball_speed_y

        # 边界碰撞检测
        if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:
            ball_speed_x *= -0.8
        if ball_y >= HEIGHT - ball_radius:
            reset_ball()

    check_score()

    # 绘制游戏元素
    draw_hoop()
    draw_ball()

    # 显示得分
    score_text = font.render(f"Score: {score}", True, RED)
    win.blit(score_text, (10, 10))

    # 绘制拖拽辅助线
    if dragging:
        mouse_pos = pygame.mouse.get_pos()
        pygame.draw.line(win, RED, (ball_x, ball_y), mouse_pos, 2)

    pygame.display.update()
    pygame.time.Clock().tick(60)

pygame.quit()



#游戏玩法说明：
#1. 用鼠标点击并拖拽篮球来调整投篮方向和力度
#2. 松开鼠标左键投出篮球
#3. 当篮球穿过篮筐时得分
#4. 篮球落地后会重置位置
#5. 拖拽线越长表示力度越大
#6. 篮球碰到边界会反弹
#
#游戏特点：
#- 使用简单的物理模拟（重力和反弹）
#- 拖拽力度线可视化
#- 得分统计功能
#- 简单的碰撞检测
#- 无需任何图片资源
#
#可以通过调整以下参数来修改游戏行为：
#- `gravity` 改变重力大小
#- `ball_radius` 改变篮球大小
#- `hoop_width` 和 `hoop_y` 调整篮筐位置和大小
#- 反弹系数（代码中的0.8）改变碰撞后的能量损失